﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Enum;

namespace br.com.SistemaChamados.DAO
{
    public class UnitDAO
    {

        private static string SQL_INSERT_UNIT = " INSERT INTO TBL_UNIT (TBL_UNITNAME) VALUES (@name) ";
        private static string SQL_UPDATE_UNIT = " UPDATE TBL_UNIT SET TBL_UNITNAME = @name WHERE TBL_UNITID = @id ";
        private static string SQL_DELETE_UNIT = " DELETE FROM TBL_UNIT WHERE TBL_UNITID = @id ";
        private static string SQL_GET_UNITS = " SELECT * FROM TBL_UNIT ";
        private static string SQL_GET_UNIT = " SELECT * FROM TBL_UNIT WHERE TBL_UNITID = @id";

        public static Unit getUnit(Unit unit)
        {
            IDbConnection conn = null;
            IDbCommand command = null;
            IDataReader reader = null;
            Unit unitReturn = new Unit();
            unitReturn.Place = new Place();
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_GET_UNIT, conn);
                command.Parameters.Add(Mapped.Parameter("@id", unit.Id));
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                reader.Read();
                unitReturn.Id = Convert.ToInt32(reader["TBL_UNITID"].ToString());
                unitReturn.Name = Convert.ToString(reader["TBL_UNITNAME"].ToString());
                unitReturn.Place.Id = Convert.ToInt32(reader["TBL_PLACEID"].ToString());
            }
            catch (Exception ex)
            {
                unitReturn = null;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return unitReturn;
        }

        public static List<Unit> getUnits()
        {
            List<Unit> list = new List<Unit>();
            IDbConnection conn = null;
            IDbCommand command = null;
            IDataReader reader = null;
            Unit unitReturn = new Unit();
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_GET_UNITS, conn);
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    unitReturn = new Unit();
                    unitReturn.Id = Convert.ToInt32(reader["TBL_UNITID"].ToString());
                    unitReturn.Name = Convert.ToString(reader["TBL_UNITNAME"].ToString());
                    list.Add(unitReturn);
                    unitReturn = null;


                }
            }
            catch (Exception ex)
            {
                list = null;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return list;
        }

        public static EnumGenericState InsertUnit(Unit unit)
        {

            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            IDbConnection conn = null;
            IDbCommand command = null;

            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_INSERT_UNIT, conn);
                command.Parameters.Add(Mapped.Parameter("@name", unit.Name));
                command.ExecuteNonQuery();
                stateTransaction = EnumGenericState.GENERIC_STATE_SUCCESS;
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return stateTransaction;

        }

        public static EnumGenericState UpdateUnit(Unit unit)
        {

            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            IDbConnection conn = null;
            IDbCommand command = null;

            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_UPDATE_UNIT, conn);
                command.Parameters.Add(Mapped.Parameter("@name", unit.Name));
                command.Parameters.Add(Mapped.Parameter("@id", unit.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumGenericState.GENERIC_STATE_SUCCESS;
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return stateTransaction;

        }

        public static EnumGenericState DeleteUnit(Unit unit)
        {
            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            IDbConnection conn = null;
            IDbCommand command = null;

            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_DELETE_UNIT, conn);
                command.Parameters.Add(Mapped.Parameter("@id", unit.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumGenericState.GENERIC_STATE_SUCCESS;
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return stateTransaction;

        }

        public UnitDAO()
        {

        }
    }
}